package com.zt.service.impl;

import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.domain.User;
import com.zt.service.UserService;
import com.zt.mapper.UserMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* @author Lenovo
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-05-09 15:04:10
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public String login(String username) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user=userMapper.selectOne(queryWrapper);
        if (user!=null){
            Map<String, Object> map=new HashMap<>();
            map.put("id",user.getId());
            String token = JWTUtil.createToken(map, "zys".getBytes());
            redisTemplate.opsForValue().set("token",token,600, TimeUnit.SECONDS);
            return token;
        }else {
            return "用户不存在";
        }
    }
}




